package com.atguigu.activemq.jdbc_persistent.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.UnsupportedEncodingException;

public class JmsProduce_Topic_persist {
    public static final String ACTIVEMQ_URL="tcp://124.70.84.192:61616";
    public static final String TOPIC_NAME="topic_jdbc";
    public static void main(String[] args) throws JMSException, UnsupportedEncodingException {
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        MessageProducer messageProducer = session.createProducer(topic);
        messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();//与queue设置持久化不同,topic是在设置完持久化之后再启动
        for (int i=1;i<3;i++){
            TextMessage textMessage = session.createTextMessage("《主题》我发送的第" + i + "条消息");
            messageProducer.send(textMessage);
        }
        messageProducer.close();
        session.close();
        connection.close();
        System.out.println("topic发布成功并连接中断");
    }
}
